package org.aeon.gamechatnest.configuration;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

/**
 * @author: MissingAeon
 * @date: 2024/5/8 18:23
 * @description: 验证器配置
 */
@Configuration
public class ValidatorConfiguration {
    @Bean
    public Validator validator() {
        // 创建一个HibernateValidator工厂
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                // 配置验证器，失败 fastFail为true表示在第一个验证失败时立即抛出异常
                .configure()
                .failFast(true)
                .buildValidatorFactory();
        // 返回验证器
        return validatorFactory.getValidator();
    }

    // 后处理器，用于在方法执行前后对请求数据进行验证。
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        // 返回一个MethodValidationPostProcessor实例
        return new MethodValidationPostProcessor();
    }
}
